<template>
  <div v-loading="loading">
    <a-form-model
      ref="ruleForm"
      :model="formData"
      :rules="rules"
    >
      <a-row>
        <a-col :span="8">
          <a-form-model-item label="验收人">
            <BaseInput
              v-model="formData.specVerifierName"
              disabled
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            label="验收时间"
            prop="acceptanceDate"
          >
            <a-date-picker
              v-model="formData.acceptanceDate"
              value-format="YYYY-MM-DD"
              :disabled="!editable"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col>
          <a-form-model-item
            label="验收记录"
            prop="acceptanceRecord"
          >
            <BaseTextarea
              v-model="formData.acceptanceRecord"
              :disabled="!editable"
              :auto-size="{ minRows: 3, maxRows: 5 }"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </div>
</template>
<script>
import { acceptanceResultEdit } from '@/api/material/check/rectification'

export default {
	name:'a'+Date.now(),
    props: {
        editable: {
            type: Boolean
        },
        row: {
            type: Object
        }
    },
    data() {
        return {
            loading: false,
            isSubmit: false,
            formData: {
                specVerifierName: '',
                acceptanceDate: null,
                acceptanceRecord: ''
            },
            rules: {
                acceptanceDate: { required: true, message: '验收时间不能为空', trigger: 'change' },
                acceptanceRecord: { required: true, message: '验收记录不能为空', trigger: 'blur' }
            }
        }
    },
    watch: {
        //这里特殊处理，因为一开进来可能就是第三步或者第二步，如果在mounted或者created里面处理的话，row.id会没有值
        'row.id'(){
            this.getEditInfo()
            this.isSubmit = true
        }
    },
    mounted() {
        if (this.row.id) {
            this.getEditInfo()
            this.isSubmit = true
        }
        this.$bus.$on('acceptanceResultsCheckStepOver', () => {
            this.temporaryStorage(true)
        })
    },
    methods: {
        // 获取编辑回显数据
        getEditInfo() {
            this.formData.specVerifierName = this.row.specVerifierName
            this.formData.acceptanceDate = this.row.acceptanceDate
            this.formData.acceptanceRecord = this.row.acceptanceRecord
        },
        temporaryStorage(isNotify) {
            this.$refs.ruleForm.validate((valid, object) => {
                if (valid) {
                    this.formData.id = this.row.id
                    this.loading = true
                    acceptanceResultEdit(this.formData).then(res => {
                        if (res.code === 200) {
                            this.row = res.data
                            this.$message.success('保存成功')
                            if (isNotify) {
                                this.$bus.$emit('handelApprovalCopy')
                            }
                            this.isSubmit = true
                        }
                        this.loading = false
                    })
                } else {
                    this.$noticeWarning('请填写完整')
                    return false
                }
            })
        }
    }
}
</script>